# coding: utf8
# datetime: 2021-03-25
# 题目地址: https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/


# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if not head:
            return head

        pre_list = ListNode(0, head)
        tmp = pre_list
        while tmp.next and tmp.next.next:
            if tmp.next.val == tmp.next.next.val:
                x = tmp.next.val
                while tmp.next and tmp.next.val == x:
                    tmp.next = tmp.next.next
            else:
                tmp = tmp.next
        return pre_list.next
